#include "bhxjglcolumn.h"

BHXJGLColumn::BHXJGLColumn():BHXJGLModel ()
{

}

void BHXJGLColumn::Init(BHXJGLFUN *glcore, unsigned int num)
{
    this->gl=glcore;
    auto arr=BHXJGLModelHelper::getCirclePoints(num);

    BHXJGLTexture2D *texture=new BHXJGLTexture2D();
    this->textures.push_back(texture);
    texture->Init(gl,"./texture/123.jpeg");

    //筒子
    {
        BHXJGLTexture2D *texture1=new BHXJGLTexture2D();
        this->textures.push_back(texture1);
        texture1->Init(gl,"./texture/122.jpg");


        BHXJGLMesh *m=new BHXJGLMesh();
        BHXJGLVertex v;
        //顶边
        for(unsigned int i=0;i<arr->size();++i)
        {
            //顶点
            v.position=arr->at(i)+glm::vec3(0,0,2);
            float s=static_cast<float>(i)/num;
            //s=s>0.5f?1-s:s;
            v.texcoodr=glm::vec2(s,1.f);
            v.texcoodr=v.texcoodr*glm::vec2(4,2);
            m->vertices.push_back(v);
        }
        //底边
        for(unsigned int i=0;i<arr->size();++i)
        {
            //顶点
            v.position=arr->at(i);
            float s=static_cast<float>(i)/num;
            //s=s>0.5f?1-s:s;
            v.texcoodr=glm::vec2(s,0.f);
            v.texcoodr=v.texcoodr*glm::vec2(4,2);
            m->vertices.push_back(v);
        }
        BHXJGLModelHelper::addStripeToMesh(m,0,arr->size(),num);

        //加入网格并初始化
        this->meshes.push_back(m);
        m->Init(this->gl);

        m->textures.push_back(texture1);
    }


    //盖子
    {
        BHXJGLMesh *m=new BHXJGLMesh();
        BHXJGLVertex v;
        //顶边
        for(unsigned int i=0;i<arr->size();++i)
        {
            //顶点
            v.position=arr->at(i)+glm::vec3(0,0,2);
            v.texcoodr=glm::vec2(v.position.x/2+0.5f,v.position.y/2+0.5f);
            m->vertices.push_back(v);
        }
        //底边
        for(unsigned int i=0;i<arr->size();++i)
        {
            //顶点
            v.position=arr->at(i);
            v.texcoodr=glm::vec2(v.position.x/2+0.5f,v.position.y/2+0.5f);
            m->vertices.push_back(v);
        }
        //顶中心
        v.position=glm::vec3(0,0,2);
        v.texcoodr=glm::vec2(0.5);
        m->vertices.push_back(v);
        //底心
        v.position=glm::vec3(0,0,0);
        v.texcoodr=glm::vec2(0.5);
        m->vertices.push_back(v);
        BHXJGLModelHelper::addPieToMesh(m,m->vertices.size()-2,0,num,BHXJFront);

        BHXJGLModelHelper::addPieToMesh(m,m->vertices.size()-1,arr->size(),num,BHXJBack);

        //加入网格并初始化
        this->meshes.push_back(m);
        m->Init(this->gl);

        m->textures.push_back(texture);
    }

}



